package com.aier.common.common;

public class ByteUtil {
	 /** 
     * 转换short为byte 
     *  
     * @param b 
     * @param s 
     *            需要转换的short 
     * @param index 
     */  
    public static void putShort(byte[] b, short s, int index) {  
        b[index + 1] = (byte) (s >> Constant.num_8);  
        b[index + 0] = (byte) (s >> 0);  
    }  
  
    /** 
     * 通过byte数组取到short 
     *  
     * @param b 
     * @param index 
     *            第几位开始取 
     * @return 
     */  
    public static short getShort(byte[] b, int index) {  
        return (short) (((b[index + 1] << Constant.num_8) | b[index + 0] & Constant.num_ff));  
    }  
  
    /** 
     * 转换int为byte数组 
     *  
     * @param bb 
     * @param x 
     * @param index 
     */  
    public static void putInt(byte[] bb, int x, int index) {  
        bb[index + Constant.num_3] = (byte) (x >> Constant.num_24);  
        bb[index + 2] = (byte) (x >> Constant.num_16);  
        bb[index + 1] = (byte) (x >> Constant.num_8);  
        bb[index + 0] = (byte) (x >> 0);  
    }  
  
    /** 
     * 通过byte数组取到int 
     *  
     * @param bb 
     * @param index 
     *            第几位开始 
     * @return 
     */  
    public static int getInt(byte[] bb, int index) {  
        int temp1 = ((bb[index + Constant.num_3] & Constant.num_ff) << Constant.num_24) | ((bb[index + 2] & Constant.num_ff) << Constant.num_16);
        int temp2 = (bb[index + 1] & Constant.num_ff) << Constant.num_8;
        return temp1 | temp2 | ((bb[index + 0] & Constant.num_ff) << 0);  
    }  
  
    /** 
     * 转换long型为byte数组 
     *  
     * @param bb 
     * @param x 
     * @param index 
     */  
    public static void putLong(byte[] bb, long x, int index) {  
        bb[index + Constant.num_7] = (byte) (x >> Constant.num_56);  
        bb[index + Constant.num_6] = (byte) (x >> Constant.num_48);  
        bb[index + Constant.num_5] = (byte) (x >> Constant.num_40);  
        bb[index + Constant.num_4] = (byte) (x >> Constant.num_32);  
        bb[index + Constant.num_3] = (byte) (x >> Constant.num_24);  
        bb[index + 2] = (byte) (x >> Constant.num_16);  
        bb[index + 1] = (byte) (x >> Constant.num_8);  
        bb[index + 0] = (byte) (x >> 0);  
    }  
  
    /** 
     * 通过byte数组取到long 
     *  
     * @param bb 
     * @param index 
     * @return 
     */  
    public static long getLong(byte[] bb, int index) {
        long temp1 = (((long) bb[index + Constant.num_7] & Constant.num_ff) << Constant.num_56)  
                | (((long) bb[index + Constant.num_6] & Constant.num_ff) << Constant.num_48);
        long temp2 = (((long) bb[index + Constant.num_5] & Constant.num_ff) << Constant.num_40)  
                | (((long) bb[index + Constant.num_4] & Constant.num_ff) << Constant.num_32);
        long temp3 = (((long) bb[index + Constant.num_3] & Constant.num_ff) << Constant.num_24)  
                | (((long) bb[index + 2] & Constant.num_ff) << Constant.num_16);
        long temp4 = (((long) bb[index + 1] & Constant.num_ff) << Constant.num_8) | (((long) bb[index + 0] & Constant.num_ff) << 0);
        return temp1 | temp2 | temp3 | temp4;  
    }  
  
    /** 
     * 字符到字节转换 
     *  
     * @param ch 
     * @return 
     */  
    public static void putChar(byte[] bb, char ch, int index) {  
        int temp = ch;  
        // byte[] b = new byte[2];  
        for (int i = 0; i < 2; i ++ ) {  
            bb[index + i] = new Integer(temp & Constant.num_ff).byteValue(); // 将最高位保存在最低位  
            temp = temp >> Constant.num_8; // 向右移8位  
        }  
    }  
  
    /** 
     * 字节到字符转换 
     *  
     * @param b 
     * @return 
     */  
    public static char getChar(byte[] b, int index) {  
        int s = 0;  
        if (b[index + 1] > 0) {  
            s += b[index + 1]; 
        } else { 
            s += Constant.num_256 + b[index + 0];
        }
        s *= Constant.num_256;  
        if (b[index + 0] > 0) {  
            s += b[index + 1];
        } else { 
            s += Constant.num_256 + b[index + 0]; 
        }
        char ch = (char) s;  
        return ch;  
    }  
  
    /** 
     * float转换byte 
     *  
     * @param bb 
     * @param x 
     * @param index 
     */  
    public static void putFloat(byte[] bb, float x, int index) {  
        // byte[] b = new byte[4];  
        int l = Float.floatToIntBits(x);  
        for (int i = 0; i < Constant.num_4; i++) {  
            bb[index + i] = new Integer(l).byteValue();  
            l = l >> Constant.num_8;  
        }  
    }  
  
    /** 
     * 通过byte数组取得float 
     *  
     * @param bb 
     * @param index 
     * @return 
     */  
    public static float getFloat(byte[] b, int index) {  
        int l;  
        l = b[index + 0];  
        l &= Constant.num_ff;  
        l |= (long) b[index + 1] << Constant.num_8;  
        l &= Constant.num_ffff;  
        l |= (long) b[index + 2] << Constant.num_16;  
        l &= Constant.num_ffffff;  
        l |= (long) b[index + Constant.num_3] << Constant.num_24;  
        return Float.intBitsToFloat(l);  
    }  
  
    /** 
     * double转换byte 
     *  
     * @param bb 
     * @param x 
     * @param index 
     */  
    public static void putDouble(byte[] bb, double x, int index) {  
        // byte[] b = new byte[8];  
        long l = Double.doubleToLongBits(x);  
        for (int i = 0; i < Constant.num_4; i++) {  
            bb[index + i] = new Long(l).byteValue();  
            l = l >> Constant.num_8;  
        }  
    }  
  
    /** 
     * 通过byte数组取得float 
     *  
     * @param bb 
     * @param index 
     * @return 
     */  
    public static double getDouble(byte[] b, int index) {  
        long l;  
        l = b[0];  
        l &= Constant.num_ff;  
        l |= (long) b[1] << Constant.num_8;  
        l &= Constant.num_ffff;  
        l |= (long) b[2] << Constant.num_16;  
        l &= Constant.num_ffffff;  
        l |= (long) b[Constant.num_3] << Constant.num_24;  
        l &= 0xffffffffL;  
        l |= (long) b[Constant.num_4] << Constant.num_32;  
        l &= 0xffffffffffL;  
        l |= (long) b[Constant.num_5] << Constant.num_40;  
        l &= 0xffffffffffffL;  
        l |= (long) b[Constant.num_6] << Constant.num_48;  
        l &= 0xffffffffffffffL;  
        l |= (long) b[Constant.num_7] << Constant.num_56;  
        return Double.longBitsToDouble(l);  
    }  
}
